﻿using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace E_mail
{
    public class Rejestr
    {
        private const string FileName = "Wstaw.reg";

        public static void Add(string path)
        {
            if (KeyExist())
                return;

            path = path.Replace("\\", "\\\\");
            string text =
                String.Format(
                    @"Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Send]

[HKEY_CLASSES_ROOT\*\shell\Send\command]
@={0}",
                    path);
            text = text.Insert(text.Length - 2, " %1");

            if (File.Exists(FileName))
            {
                try
                {
                    File.Delete(FileName);
                }
                catch (Exception)
                {
                    return;
                }
            }
            File.WriteAllText(FileName, text);

            try
            {
                Process.Start(new ProcessStartInfo(FileName)).WaitForExit();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            finally
            {
                File.Delete(FileName);
            }
        }

        public static void Remove()
        {
            if (!KeyExist())
                return;

            const string text = "Windows Registry Editor Version 5.00\n\n[-HKEY_CLASSES_ROOT\\*\\shell\\Send\\command]\n\n[-HKEY_CLASSES_ROOT\\*\\shell\\Send]";

            if (File.Exists(FileName))
                try
                {
                    File.Delete(FileName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return;
                }

            File.WriteAllText(FileName, text);

            try
            {
                Process.Start(new ProcessStartInfo(FileName)).WaitForExit();
            }
            catch (Exception)
            {
                
            }
            finally
            {
                File.Delete(FileName);
            }
        }

        public static bool KeyExist()
        {
            return Registry.ClassesRoot.OpenSubKey("*\\Shell\\Send") != null;
        }
    }
}